#include "VideoPlayList.h"

VideoPlayList::VideoPlayList(QObject * parent) : QObject(parent)
{
	currentIndex = -1;
	playList.clear();
	emit playListChanged();
	emit currentIndexChanged(currentIndex);
}

void VideoPlayList::addToPlayList(QList<QUrl> urls)
{
	playList.append(urls);
	emit playListChanged();
	if(currentIndex == -1)
	{
		currentIndex = 0;
		emit currentIndexChanged(currentIndex);
	}
}

void VideoPlayList::removeFromPlayList(int from, int to)
{
	from = qMax(from, 0);
	to = qMin(to, playList.size() - 1);
	if(from > to) return;

	playList.remove(from, to - from + 1);
	if(currentIndex < from)
	{
		emit playListChanged();
		return;
	}
	else if(currentIndex > to)
	{
		currentIndex -= to - from + 1;
	}
	else
	{
		currentIndex = -1;
	}

	emit playListChanged();
	emit currentIndexChanged(currentIndex);
}

QUrl VideoPlayList::getCurrentUrl()
{
	if(currentIndex < 0 || currentIndex >= playList.size())
		return QUrl();
	return playList.at(currentIndex);
}

int VideoPlayList::getCurrentIndex()
{
	return currentIndex;
}

void VideoPlayList::setCurrentUrl(int indx)
{
	currentIndex = indx;
	emit VideoPlayList::currentIndexChanged(currentIndex);
}

QUrl VideoPlayList::playNext()
{
	currentIndex = (currentIndex + 1) % playList.size();
	if(playList.size() > 1) currentIndexChanged(currentIndex);
	return getCurrentUrl();
}

QUrl VideoPlayList::playPrevious()
{
	currentIndex = (currentIndex - 1 + playList.size()) % playList.size();
	if(playList.size() > 1) currentIndexChanged(currentIndex);
	return getCurrentUrl();
}
int VideoPlayList::getSize()
{
	return playList.size();
}

QList<QUrl> VideoPlayList::getList()
{
	return playList;
}

